//
//  YDHomeMarketView.m
//  siyanone
//
//  Created by LK-IOS on 2018/3/23.
//  Copyright © 2018年 LK-News. All rights reserved.
//

#import "YDHomeMarketView.h"
#import "YDHomeMarketCollectionCell.h"
#import "YDHomeMarketModel.h"


@interface YDHomeMarketView()<UICollectionViewDelegate , UICollectionViewDataSource>

//显示大盘信息的collectionview
@property (nonatomic , weak) UICollectionView *collectionView;


@end


@implementation YDHomeMarketView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        //添加子控件
        [self collectionView];
        
    }
    return self;
}
-(void)setMarketModelArray:(NSArray *)marketModelArray
{
    _marketModelArray = marketModelArray;
    
    [self.collectionView reloadData];
    
}
+(instancetype)homeMarketViewMarketModelArray:(NSArray *)marketModelArray
{
    YDHomeMarketView *marketView = [[self alloc] init];
    marketView.marketModelArray = marketModelArray;
    return marketView;
}


static NSString * const homeMarketCollectionCell = @"YDHomeMarketCollectionCell";
-(UICollectionView *)collectionView
{
    if (_collectionView == nil) {
        
        //collectionView布局
        UICollectionViewFlowLayout *flowLayout = ({
            //cell左右间距
            CGFloat cellMarginLR = rate375(20);
            CGFloat cellH;
            if (isPhoneX) {
                cellH = rateX812(79);
            }else{
                cellH = rate750(79);
            }
            UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
            //cell的大小,宽和高均为屏幕的四分之一
            flowLayout.itemSize = CGSizeMake((LKScreenW - cellMarginLR * 4) / 3, cellH);
            
            flowLayout.sectionInset = UIEdgeInsetsMake(0, cellMarginLR, 0, cellMarginLR);
            //行间距
            flowLayout.minimumLineSpacing = cellMarginLR;
            //cell的间距
            flowLayout.minimumInteritemSpacing = cellMarginLR;//列之间的间距
            //设置滚动方向
            flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
            
            flowLayout;
        });
        //创建collectionView,直接创建显示不出来,必须要有流水布局才可以
        UICollectionView *collectionView = ({
            UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.frame collectionViewLayout:flowLayout];
            [self addSubview:collectionView];
            [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.bottom.equalTo(self);
            }];
            //设置背景色
            collectionView.backgroundColor = [UIColor whiteColor];
            
            //取消垂直滚动条
            collectionView.showsVerticalScrollIndicator = NO;
            collectionView.showsHorizontalScrollIndicator = NO;
            collectionView.shouldGroupAccessibilityChildren = NO;
            //设置数据源及代理
            collectionView.dataSource = self;
            collectionView.delegate = self;
            collectionView;
        });
        [collectionView registerClass:[YDHomeMarketCollectionCell class] forCellWithReuseIdentifier:homeMarketCollectionCell];
        
        _collectionView = collectionView;
        
    }
    return _collectionView;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.marketModelArray.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YDHomeMarketCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homeMarketCollectionCell forIndexPath:indexPath];

    
    YDHomeMarketModel *model = self.marketModelArray[indexPath.row];
    cell.model = model;
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.delegate respondsToSelector:@selector(homeMarketView:didSelectItemAtIndex:)]) {
        [self.delegate homeMarketView:self didSelectItemAtIndex:indexPath.item];
    }
}

@end
